/*
 * elf related lib
 *
 */
#include "elf.h"

int is_elf(Elf32_Ehdr*elf_hdr){
	if (!IS_ELF( *elf_hdr)){
		return 0;
	}

	return 1;
}

Elf32_Addr elf_entry(Elf32_Ehdr *elf_hdr){
	return elf_hdr->e_entry;
}
	
Elf32_Off get_offset(Elf32_Addr p_hdr,Elf32_Addr entry,Elf32_Half p_size, Elf32_Half p_num){
	Elf32_Phdr *elf_phdr;
	Elf32_Off  offset;
	int i;

	for ( i = 0;  i < p_num;  i++, p_hdr += p_size){
		elf_phdr = (Elf32_Phdr *)p_hdr;

		if (elf_phdr->p_type != PT_LOAD){
			continue ;
		}
		
		if (( entry >= elf_phdr->p_vaddr) && 
			( entry <= (elf_phdr->p_vaddr + elf_phdr->p_memsz))){
			break;
		}
	}

	if ( i == p_num){
		return INVALID;
	}

	offset = entry - elf_phdr->p_vaddr + elf_phdr->p_offset;
	
	return offset;
}

Elf32_Off elf_read(Elf32_Ehdr *elf_hdr){
	if (!elf_hdr){
		return 0;
	}
	
	if (!is_elf(elf_hdr)){
		return 0;
	}

	switch(elf_hdr->e_type){
	case ET_NONE:
	{
		/* do nothing */
		return 0;
	}
	case ET_REL:
	{
		/* do nothing */
		return 0;
	}
	case ET_EXEC:
	{
		Elf32_Addr *p_hdr =  (Elf32_Addr)elf_hdr + (Elf32_Off)elf_hdr->e_phoff;
		
		return get_offset(p_hdr, elf_hdr->e_entry, 
			elf_hdr->e_phentsize,  elf_hdr->e_phnum);
	}
	case ET_DYN:
	{
		/* do nothing */
		return 0;
	}
	case ET_CORE:
	{
		/* do nothing */
		return 0;
	}
	case ET_LOOS:
	{
		/* do nothing */
		return 0;
	}
	case ET_HIOS:
	{
		/* do nothing */
		return 0;
	}
	case ET_LOPROC:
	{
		/* do nothing */
		return 0;
	}
	case ET_HIPROC:
	{
		/* do nothing */
		return 0;
	}
	default:
		return 0;
	}/* end of switch(elf_hdr->e_type)*/
}

int elf_loader(void){
	return 0;
}

/* EOF  */

